<?php
namespace app\driver\logic;
use app\driver\BaseModel;
use think\Cache;
use think\Request;
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:30
 */
class Login extends BaseModel
{

    //检测参数是否已传
    protected $ParameterDetection  = [
        'login'=>['mobile','code'],
        'forgetpasswd'=>['mobile','code'],
        'registered'=>['mobile', 'code', 'password', 'nickname', 'address', 'idcard', 'drivercard', 'cart_number']
    ];

    /**
 * 用户登陆（手机验证）
 * @return array
 */
    public function login()
    {
        $condition['mobile'] = $this->requestData['mobile'];
        $condition['status'] = 1;
        $root = db('driver')->where($condition)->find();
        if(!$root) return array('message'=>'账号不存在');
        if($root['status'] != 1) return array('message'=>'账号异常');
        if(Cache::get($root['mobile']) != base64_encode($this->requestData['code'])) return array('message' => '验证码不正确错误');
        unset($root['passwd']);
        $Oauth2 =  new \app\driver\service\Oauth2();
        $user=array(
            "id"=>$root['id'],
        );
        //返回生成token
        return $Oauth2::generateToken($user);
    }

    /**
     * 用户登陆（密码）
     * @return array
     */
    public function secretlogin()
    {
        $passwd = $this->requestData['password'];
        $condition['mobile'] = $this->requestData['mobile'];
        $condition['status'] = 1;
        $root = db('driver')->where($condition)->find();
        if(!$root) return array('message'=>'账号不存在');
        if($root['status'] != 1) return array('message'=>'账号异常');
        if($root['passwd'] != md5($passwd))return array('message'=>'密码错误');
 //       if(Cache::get($root['mobile']) != base64_encode($this->requestData['code'])) return array('message' => '验证码不正确错误');
        unset($root['passwd']);
        $Oauth2 =  new \app\driver\service\Oauth2();
        $user=array(
            "id"=>$root['id'],
        );
        //返回生成token
        return $Oauth2::generateToken($user);
    }

    //忘记密码
    public function forgetpasswd()
    {
        $mobile = trim($this->requestData['mobile']);
        $code = trim($this->requestData['code']);
        $password = trim($this->requestData['password']);
        $password = md5($password);
        $repassword = trim($this->requestData['repassword']);
//        if(Cache::get($root['mobile']) != base64_encode($this->requestData['code'])) return array('message' => '验证码不正确错误');

        $where['mobile'] = $mobile;
        $root = db('driver')->where($where)->find();
        if(!$root) return array('message'=>'不存在此账户');

        if($password != $repassword)return array('message'=>'密码不一致');
        $data=[
            'passwd'=>$password,
        ];
        if(!db('driver')->where(['id'=>$this->user['id']])->update($data))return ['message'=>'修改密码失败'];
        return array();
    }

    //注册
    public function registered(){
        $mobile = $this->requestData['mobile'];
        $code = trim($this->requestData['code']);
        $password = trim($this->requestData['password']);
        $nickname = trim($this->requestData['nickname']);
//        $province = trim($this->requestData['province']);
//        $city = trim($this->requestData['city']);
//        $county = trim($this->requestData['county']);
        $address = trim($this->requestData['address']);
        $idcard = trim($this->requestData['idcard']);
        $drivercard = trim($this->requestData['drivercard']);
        $cart_number = trim($this->requestData['cart_number']);

        //if(Cache::get("code") != base64_encode($this->requestData['code'])) return array('message' => '验证码不正确错误');
        if(empty($mobile) || empty($password) || empty($nickname) || empty($address) || empty($idcard) || empty($drivercard) || empty($cart_number))return ['message'=>'有信息漏填'];

        $result = db('driver')->where(['mobile'=>$mobile])->find();
        if(!empty($result))return ['message'=>'该手机已注册!'];

        $result = db('driver')->where(['nickname'=>$nickname])->find();
        if(!empty($result))return ['message'=>'该昵称已注册!'];

        $result = db('driver')->where(['idcard_number'=>$idcard])->find();
        if(!empty($result))return ['message'=>'该身份证已注册!'];

        $result = db('driver')->where(['driving_number'=>$drivercard])->find();
        if(!empty($result))return ['message'=>'该驾驶证已注册!'];

        $result = db('driver')->where(['cart_number'=>$cart_number])->find();
        if(!empty($result))return ['message'=>'该车牌号已注册!'];

        $add_driver_date['passwd'] = md5($password);
        $add_driver_date['mobile'] = $mobile;
        $add_driver_date['nickname'] = $nickname;
        $add_driver_date['address'] = $address;
        $add_driver_date['idcard_number'] = $idcard;
        $add_driver_date['driving_number'] = $drivercard;
        $add_driver_date['cart_number'] = $cart_number;
        $add_driver_date['create_time'] = time();

        $uid = db('driver')->insert($add_driver_date);
        if(empty($uid))return ['message'=>'注册失败!'];
        return array();
    }
}